﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace UnityTouchTable.Example
{
    /// <summary>
    /// 线条节点
    /// </summary>
    [RequireComponent(typeof(LineRenderer))]
    [DisallowMultipleComponent]
    public class LineNode : MonoBehaviour
    {
        #region Field
        /// <summary>
        /// 父控制器
        /// </summary>
        [SerializeField]
        [Header("父控制器")]
        private MarkerItem m_MarkEffect = default;

        /// <summary>
        /// 线条渲染器
        /// </summary>
        [SerializeField]
        [Header("线条渲染器")]
        private LineRenderer m_LineRenderer = default;

        /// <summary>
        /// 渐变周期
        /// </summary>
        [SerializeField]
        [Header("渐变周期")]
        [Range(0.5f, 5.0f)]
        private float m_GradientDuration = 3.0f;

        /// <summary>
        /// 渐变颜色
        /// </summary>
        [SerializeField]
        [Header("渐变颜色")]
        private Gradient m_Gradient = default;

        /// <summary>
        /// 线条点位
        /// </summary>
        private Vector3[] m_LinePostions = new Vector3[2];

        /// <summary>
        /// 计时器
        /// </summary>
        private float m_Timer;
        #endregion

        #region Function
        public void Update()
        {
            if (this.m_MarkEffect.CurrentSubContentNode == null)
                return;

            this.m_Timer += Time.deltaTime;
            float ratio = this.m_Timer / this.m_GradientDuration;
            if (ratio >= 1.0f)
            {
                ratio = 1.0f;
                this.m_Timer = 0.0f;
            }

            this.m_LinePostions[0] = this.m_MarkEffect.transform.position;
            this.m_LinePostions[1] = this.m_MarkEffect.CurrentSubContentNode.transform.position;
            this.m_LineRenderer.positionCount = 2;
            this.m_LineRenderer.endColor = this.m_Gradient.Evaluate(ratio); ;
            this.m_LineRenderer.SetPositions(this.m_LinePostions);
        }
        #endregion
    }
}